﻿Imports SecretaryToolData

Public Class frmGroups

	Private ReadOnly Property workingGroup As VM_Group
		Get
			If cmboGroups.SelectedValue IsNot Nothing Then
				Return New VM_Group(cmboGroups.SelectedValue)
			Else
				Return Nothing
			End If
		End Get
	End Property

	Private Sub frmGroups_Load(sender As Object, e As System.EventArgs) Handles Me.Load
		PopulateOverseers()
		PopulateGroups()
		PopulateAllPublishers()
		PopulateGroupPublishers()
	End Sub

	Private Sub PopulateGroups()
		Dim groupCollection As VM_Group() = VM_Group.AllGroups.ToArray

		cmboGroups.DisplayMember = Extensions.GetMemberInfo(Function(x As VM_Group) x.GroupName).Name
		cmboGroups.ValueMember = Extensions.GetMemberInfo(Function(x As VM_Group) x.GroupID).Name
		cmboGroups.DataSource = groupCollection

	End Sub

	Private Sub PopulateOverseers()
		Dim overseerCollection As VM_PublisherInformation() = VM_Group.PotentialOverseers
		Dim assistantCollection As VM_PublisherInformation() = VM_Group.PotentialOverseers

		cmboOverseer.DisplayMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.FullName).Name
		cmboOverseer.ValueMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.PublisherID).Name
		cmboOverseer.DataSource = overseerCollection

		cmboAssistant.DisplayMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.FullName).Name
		cmboAssistant.ValueMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.PublisherID).Name
		cmboAssistant.DataSource = assistantCollection
	End Sub

	Private Sub PopulateAllPublishers()
		Dim publisherCollection As VM_PublisherInformation() = VM_Group.UnGroupedPublishers.ToArray

		lstPublishers.DataSource = publisherCollection
		lstPublishers.DisplayMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.FullName).Name
		lstPublishers.ValueMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.PublisherID).Name

		'http://stackoverflow.com/questions/14689397/listbox-with-auto-sizing-horizontal-scroll-bar
		Dim widthFinder As Graphics = lstPublishers.CreateGraphics()
		Dim needScrollWidth As Integer = 0
		Dim checkVal As Integer = 0
		For i As Integer = 0 To publisherCollection.Count - 1
			checkVal = CInt(widthFinder.MeasureString(publisherCollection(i).FullName, lstPublishers.Font).Width) + 1
			If needScrollWidth < checkVal Then
				needScrollWidth = checkVal
			End If
		Next

		lstPublishers.HorizontalScrollbar = True
		lstPublishers.HorizontalExtent = needScrollWidth
		lstPublishers.ColumnWidth = needScrollWidth
		lstPublishers.Invalidate()

	End Sub

	Private Sub PopulateGroupPublishers()
		If workingGroup IsNot Nothing Then
			Dim publisherCollection As VM_PublisherInformation() = workingGroup.GroupMembers.ToArray

			lstGroupedPublishers.DataSource = publisherCollection
			lstGroupedPublishers.DisplayMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.FullName).Name
			lstGroupedPublishers.ValueMember = Extensions.GetMemberInfo(Function(x As VM_PublisherInformation) x.PublisherID).Name

			'http://stackoverflow.com/questions/14689397/listbox-with-auto-sizing-horizontal-scroll-bar
			Dim widthFinder As Graphics = lstGroupedPublishers.CreateGraphics()
			Dim needScrollWidth As Integer = 0
			Dim checkVal As Integer = 0
			For i As Integer = 0 To publisherCollection.Count - 1
				checkVal = CInt(widthFinder.MeasureString(publisherCollection(i).FullName, lstPublishers.Font).Width) + 1
				If needScrollWidth < checkVal Then
					needScrollWidth = checkVal
				End If
			Next

			lstGroupedPublishers.HorizontalScrollbar = True
			lstGroupedPublishers.HorizontalExtent = needScrollWidth
			lstGroupedPublishers.ColumnWidth = needScrollWidth
			lstGroupedPublishers.Invalidate()

		End If
	End Sub

	Private Sub cmboGroups_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cmboGroups.SelectedValueChanged
		If cmboGroups.SelectedValue IsNot Nothing Then
			PopulateGroupPublishers()
			cmboOverseer.SelectedValue = workingGroup.OverseerID
			cmboAssistant.SelectedValue = workingGroup.AssistantID
		End If
	End Sub

	Private Sub btnAddToGroup_Click(sender As Object, e As System.EventArgs) Handles btnAddToGroup.Click
		'check to make sure something is selected
		If lstPublishers.SelectedIndices.Count > 0 Then
			For Each publisher As VM_PublisherInformation In lstPublishers.SelectedItems
				workingGroup.AddMember(publisher.PublisherID)
			Next
		End If
		PopulateAllPublishers()
		PopulateGroupPublishers()
	End Sub

	Private Sub btnRemoveFromGroup_Click(sender As Object, e As System.EventArgs) Handles btnRemoveFromGroup.Click
		'check to make sure something is selected
		If lstGroupedPublishers.SelectedIndices.Count > 0 Then
			For Each publisher As VM_PublisherInformation In lstGroupedPublishers.SelectedItems
				workingGroup.RemoveMember(publisher.PublisherID)
			Next
		End If
		PopulateAllPublishers()
		PopulateGroupPublishers()
	End Sub

	Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
		Me.Close()
	End Sub

	Private Sub btnAddGroup_Click(sender As System.Object, e As System.EventArgs) Handles btnAddGroup.Click
		Dim tempGroup = New VM_Group

		PopulateGroups()
	End Sub

	Private Sub btnRenameGroup_Click(sender As System.Object, e As System.EventArgs) Handles btnRenameGroup.Click
		workingGroup.GroupName = InputBox("Enter new group name:", "Rename Group")

		PopulateGroups()
	End Sub

	Private Sub btnDeleteGroup_Click(sender As System.Object, e As System.EventArgs) Handles btnDeleteGroup.Click
		workingGroup.Delete()
	End Sub

	Private Sub cmboOverseer_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cmboOverseer.SelectedValueChanged
		If workingGroup IsNot Nothing AndAlso cmboOverseer.SelectedValue IsNot Nothing Then
			workingGroup.OverseerID = cmboOverseer.SelectedValue
		End If
	End Sub

	Private Sub cmboAssistant_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cmboAssistant.SelectedValueChanged
		If workingGroup IsNot Nothing AndAlso cmboAssistant.SelectedValue IsNot Nothing Then
			workingGroup.AssistantID = cmboAssistant.SelectedValue
		End If
	End Sub

	Private Sub btnGenerateGroupReport_Click(sender As System.Object, e As System.EventArgs) Handles btnGenerateGroupReport.Click

		If MessageBox.Show(String.Format("Do you want to report on all in the group [Yes],{0}or just the selected members [No]?", Environment.NewLine), "All or Some?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
			Dim publisherList As New List(Of VM_PublisherInformation)
			For Each publisher In lstPublishers.SelectedIndices
				publisherList.Add(CType(lstPublishers.Items(publisher), VM_PublisherInformation))
			Next
			For Each publisher In lstGroupedPublishers.SelectedIndices
				publisherList.Add(CType(lstGroupedPublishers.Items(publisher), VM_PublisherInformation))
			Next
			MessageBox.Show(String.Format("Report generated and saved at{0}{1}", Environment.NewLine, Reports.GeneratePublishersReport(publisherList)), "Report Successfully Generated", MessageBoxButtons.OK, MessageBoxIcon.Information)
		Else
			MessageBox.Show(String.Format("Report generated and saved at{0}{1}", Environment.NewLine, Reports.GenerateGroupReport(cmboGroups.SelectedValue)), "Report Successfully Generated", MessageBoxButtons.OK, MessageBoxIcon.Information)
		End If

	End Sub
End Class